<template>
  <van-datetime-picker
    v-model="currentDate"
    type="date"
    title="选择年月日"
    :min-date="minDate"
    :max-date="maxDate"
    @cancel="$emit('close')"
    @confirm="onConfirm"
  />
</template>

<script>
import { updateUserProfile } from "@/api/user.js";
import dayjs from "dayjs";
export default {
  props: {
    value: {
      type: String,
    },
  },
  data() {
    return {
      minDate: new Date(1970, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(this.value),
    };
  },
  methods: {
    async onConfirm() {
      this.$toast.loading({
        message: "保存中...",
        forbidClick: true,
        duration: 0,
      });
      try {
        // 格式化时间
        const currentDate = dayjs(this.currentDate).format("YYYY-MM-DD");
        console.log(currentDate);
        // 发起请求 改什么传什么参数
        await updateUserProfile({
          birthday: currentDate,
        });
        // 传给父组件中的v-model
        this.$emit("input", currentDate);
        // 退出弹出层
        this.$emit("close");
        this.$toast.success("更新成功");
      } catch (err) {
        if (err.response && err.response.status === 401) {
          return this.$toast("用户未认证");
        }
        this.$toast("更新失败");
      }
    },
  },
};
</script>

<style>
</style>